package com.momochen.service;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

/**
 * @author 徐飞
 * @version 2016/02/25 12:00
 */
public class NettyServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        // channel失效，从Map中移除
//		NettyChannelMap.remove((SocketChannel) ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        super.exceptionCaught(ctx, cause);
        System.out.println("服务器端出现异常！");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket packet) throws Exception {
        System.out.println("=>" + packet.content().toString(CharsetUtil.UTF_8) + "  " + channelHandlerContext.channel().id());
    }

//    @Override
//    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
//        System.out.println("=>" + msg+"  "+channelHandlerContext.channel().id());
//        ReferenceCountUtil.release(msg);
//    }

//    @Override
//    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
//        System.out.println("=>" +o + "  " + channelHandlerContext.channel().id());
//        ReferenceCountUtil.release(o);
////        byte[] bytes = (byte[]) o;
////        System.out.println("=>" + new String(bytes) + "  " + channelHandlerContext.channel().id());
////        ReferenceCountUtil.release(o);
////
////        channelHandlerContext.writeAndFlush("momochen".getBytes());
//    }
}